package com.alipay.app.groupmeal.standard.ui.b.function.amount;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.constant.BaseConstant;
import com.alipay.app.groupmeal.standard.ui.b.util.DeviceInfoManager;
import com.alipay.app.groupmeal.standard.widget.business.BaseBScreenView;
import com.alipay.app.groupmeal.standard.widget.business.FakeToastView;

public class BScreenAmount extends BaseBScreenView {

    //自由输入金额
    private BScreenInputAmount inputAmount;
    //固定金额
    private BScreenFixedAmount fixedAmount;
    //入场核销
    private BScreenHexiaoAmount hexiaoAmount;

    //等待付款
    private BScreenAwaitPay await_pay;

    private FakeToastView mToastViewAmount;

    private AmountKeyEventListener mAmountKeyEventListener;

    private HexiaoEventListener hexiaoEventListener;


    public void setHexiaoEventListener(HexiaoEventListener hexiaoEventListener) {
        this.hexiaoEventListener = hexiaoEventListener;
    }

    public BScreenAmount(Context context) {
        super(context);
    }

    public BScreenAmount(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_amount;
    }

    @Override
    protected void initView(View view) {
        inputAmount = findViewById(R.id.input);
        fixedAmount = findViewById(R.id.fixed);
        await_pay = findViewById(R.id.await_pay);
        hexiaoAmount = findViewById(R.id.hexiao);
        mToastViewAmount = findViewById(R.id.toastViewAmount);
    }

    @Override
    protected void initData() {
    }

    @Override
    public boolean onKeyEventNum(KeyEvent event) {
        //自由金额
        if (inputAmount != null && inputAmount.isVisibility()) {
            return inputAmount.dispatchKeyEvent(event);
        }
        return super.onKeyEventNum(event);
    }

//
//    public String getAmountForCardPay() {
//        if (fixedAmount != null && fixedAmount.isVisibility()) {
//            return DeviceInfoManager.getFixedAmountString();
//
//        }
//        if (inputAmount != null && inputAmount.isVisibility()) {
//            return await_pay.getAmount().toString();
//
//        }
//        return "0";
//    }

    /**
     * 交易时回车按钮触发
     *
     * @param event
     * @return
     */
    @Override
    public boolean onKeyEventEnter(KeyEvent event) {
        //固定金额
        if (fixedAmount != null && fixedAmount.isVisibility()) {
            String amount = DeviceInfoManager.getFixedAmountString();
            if (TextUtils.isEmpty(amount) || Float.parseFloat(amount) <= 0) {
                //收款金额需大于0.00元
                showToast(getContext().getString(R.string.amount_less_than_zero));
                return false;
            }
            showAwaitPayView("等待支付", amount);
            if (mAmountKeyEventListener != null) {
                mAmountKeyEventListener.onEnterKey(amount, false);
            }
        }
        //自由金额
        if (inputAmount != null && inputAmount.isVisibility()) {
            String amount = inputAmount.getAmount().toString();
            if (TextUtils.isEmpty(amount) || Float.parseFloat(amount) <= 0) {
                //收款金额需大于0.00元
                showToast(getContext().getString(R.string.amount_less_than_zero));
                return false;
            }
            showAwaitPayView("等待支付", amount);
            if (mAmountKeyEventListener != null) {
                mAmountKeyEventListener.onEnterKey(amount, true);
            }
        }
        //核销模式
        if (hexiaoAmount != null && hexiaoAmount.isVisibility()) {
            //核销模式
            if (!hexiaoAmount.isRuns()) {
                hexiaoAmount.runHexiao();
                if (hexiaoEventListener != null) {
                    hexiaoEventListener.hexiaoClick();
                }
            }

        }
        return super.onKeyEventEnter(event);
    }

    @Override
    public boolean onKeyEventUpAndDown(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
            //自由金额
            if (inputAmount != null && inputAmount.isVisibility()) {
                if (mAmountKeyEventListener != null) {
                    mAmountKeyEventListener.onDownKey(true);
                }
                return false;
            }
        }
        return super.onKeyEventUpAndDown(event);
    }

    @Override
    public boolean onKeyEventBack(KeyEvent event) {
        if (await_pay != null && await_pay.isVisibility()) {
            await_pay.dismiss();
        } else if (hexiaoAmount != null && hexiaoAmount.isVisibility()) {
            if (hexiaoAmount.isRuns()) {
                hexiaoAmount.runHexiao();
            }
        }
        return super.onKeyEventBack(event);
    }

    @Override
    public boolean onKeyEventFunction(KeyEvent event) {
        if (mAmountKeyEventListener != null)
            mAmountKeyEventListener.onFunctionKey();
        dismiss();
        return super.onKeyEventFunction(event);
    }

    /**
     * 固定金额
     */
    public void showFixedAmount() {
        showFixedAmountInternal();
        show();
    }

    private void showFixedAmountInternal() {
        String amount = DeviceInfoManager.getFixedAmountString();
        fixedAmount.setFixedAmount(amount);
        fixedAmount.show();
        inputAmount.dismiss();
        hexiaoAmount.dismiss();
        await_pay.dismiss();
    }

    /**
     * 自由金额
     */
    public void showInputAmount() {
        showInputAmountInternal();
        show();
    }

    private void showInputAmountInternal() {
        inputAmount.show();
        fixedAmount.dismiss();
        hexiaoAmount.dismiss();
        await_pay.dismiss();
    }

    /**
     * 入场核销
     */
    public void showHexiaoAmount() {
        showHexiaoAmountInternal();
        show();
    }

    private void showHexiaoAmountInternal() {
        inputAmount.dismiss();
        fixedAmount.dismiss();
        hexiaoAmount.show();
        await_pay.dismiss();
        if (hexiaoAmount.isRuns()) {
            hexiaoAmount.runHexiao();
        }
    }

    /**
     * 等待支付-支付中
     */
    public void showAwaitPayView(String msg, String amount) {
        await_pay.setTitle(msg);
        await_pay.setAmount(amount);
        fixedAmount.dismiss();
        inputAmount.dismiss();
        await_pay.show();
    }

    /**
     * 等待支付-支付中
     */
    public void showAwaitPayView(String msg) {
        await_pay.setTitle(msg);
        await_pay.show();
    }

    public void changeSecret() {
        String mode = DeviceInfoManager.getCashierMode();
        if (mode.equals(BaseConstant.CASHIER_MODE.MODE_FIXED)) {
            showFixedAmountInternal();
        } else if (mode.equals(BaseConstant.CASHIER_MODE.MODE_FREE)) {
            showInputAmountInternal();
        } else {

        }
    }

    public BScreenHexiaoAmount getHexiaoAmount() {
        return hexiaoAmount;
    }

    public BScreenAwaitPay getAwait_pay() {
        return await_pay;
    }

    public void setAmountKeyEventListener(AmountKeyEventListener amountKeyEventListener) {
        this.mAmountKeyEventListener = amountKeyEventListener;
    }

    public interface AmountKeyEventListener {
        /**
         * @param amount 订单金额
         * @param isFree 自由模式
         */
        void onEnterKey(String amount, boolean isFree);

        void onDownKey(boolean isFree);

        void onFunctionKey();

    }

    public interface HexiaoEventListener {
        void hexiaoClick();
    }

    public void setClearAmount(boolean clearAmount) {
        inputAmount.setClearAmount(clearAmount);
    }

    public void clearAmount() {
        await_pay.dismiss();
        inputAmount.setClearAmountForce();
    }

    public void showToast(String s) {
        mToastViewAmount.showToastAndInterval(s);
    }
}
